<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta charset="UTF-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
<title>创建课程</title>
<%@ include file="/common/commonLib.jsp"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/css/newCourse.css'/>" />

</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-12 content">
				<div class="row">
					<div class="col-xs-12">
						<div class="newCourse-label">课程基本信息</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-3"></div>
					<div class="col-xs-6 newCourse-con">
						<form>
							<div class="form-group">
								<div class="row">
									<div class="col-xs-4">
										<label for="courseName">课程名</label>
									</div>
									<div class="col-xs-8">
										<input type="text" class="form-control" name="name">
									</div>
								</div>
							</div>
							<div class="form-group"> 
								<div class="row">
									<div class="col-xs-4">
										<label for="courseCode">课程代码</label>
									</div>
									<div class="col-xs-8">
										<input type="text" class="form-control" name="courseCode">
									</div>
								</div>
							</div> 
							<div class="form-group">
								<div class="row">
									<div class="col-xs-4">
										<label for="time">课时</label>
									</div>
									<div class="col-xs-8">
										<input type="text" class="form-control" name="courseHour">
									</div>
								</div>
							</div>
							<div class="form-group">
								<div class="row">
									<div class="col-xs-4">
										<label for="credit">学分</label>
									</div>
									<div class="col-xs-8">
										<input type="text" class="form-control" name="credit">
									</div>
								</div>
							</div>
							<div class="form-group">
								<div class="row">
									<div class="col-xs-4">
										<label for="totalWeek">总周数</label>
									</div>
									<div class="col-xs-8">
										<input type="text" class="form-control" name="sumWeeks">
									</div>
								</div>
							</div>
							<div class="form-group">
								<div class="row">
									<div class="col-xs-4">
										<label for="description">课程图片</label>
										<img src="../image/upload-file.png" id="img" >
										<input type="file" name="file" id="file-name" />
										<input type="hidden"  name="fileKey" />
									</div>
									<div class="col-xs-8">
										<div class="row">
											<div class="col-xs-12">
												<label for="description">课程描述</label>
											</div>
											<div class="col-xs-12">
												<div id="description" name="description" contenteditable="true" ></div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</form>
						<div class="btn-loc">
							<input type="button" class="btn btn-primary" id="addCourse" style="display:none;" value="确定" >
						</div>
					</div>
					<div class="col-xs-3"></div>
				</div>
			</div>
		</div>
	</div>
</body>
<script>
var keyList= [];
var lastKey = "";
$(function() {
	$("#addCourse").on("click", addCourse);
	$("#img").click(function() {		
		$("#file-name").click();
		$("#file-name").change(function(){
			var objUrl = getObjectURL(this.files[0]) ;
		    if (objUrl) {
		    	var key = $("input[name='fileKey']").val();
		    	if(key != ""|| key!=lastKey){
		    		lastKey = key;
		    		keyList.push(lastKey);
		    	}
		        $("#img").attr("src", objUrl);
		        ajaxFileUpload();
		    }
		});
	});
});

//	图片预览
function getObjectURL(file) {
    var url = null ; 
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}
	
function ajaxFileUpload() {
	$.ajaxFileUpload({
		url : "<c:url value='/resource/upload'/>", //用于文件上传的服务器端请求地址
		secureuri : false, //是否需要安全协议，一般设置为false
		fileElementId : 'file-name', //文件上传域的ID
		dataType : 'json', //返回值类型 一般设置为json
		success : function(data, status) //服务器成功响应处理函数
		{
			if(data.status=="success"){
				$("input[name='fileKey']").val(data.data.key);
				$("#addCourse").show();
			}else{
				alert(data.message);
			}			 
		},
		error : function(data, status, e)//服务器响应失败处理函数
		{
			alert(e);
		}
	})
	return false;
}

var removeUsing = function(){
	keyList.push($("input[name='fileKey']").val());
	return keyList;
}
var deleteBefore = function(list){
	if(list.length > 0){
		$.post(CONTEXT_PATH+"resource/delete?list="+list, function(ddata) {
			if(ddata.status=="success"){
		  		console.log("success");
			}else{
				layer.msg("not important wrong");
			}
		});
		
	}
}

var addCourse = function() {
		var courseCode = $("input[name='courseCode']").val();
	var name = $("input[name='name']").val();
	var courseHour = $("input[name='courseHour']").val();
	var credit = $("input[name='credit']").val();
	var sumWeeks = $("input[name='sumWeeks']").val();
	var description = $("#description").text();
	var image=$("input[name='fileKey']").val();

	var json = {
		"courseCode" : courseCode,
		"name" : name,
		"courseHour" : courseHour,
		"credit" : credit,
		"sumWeeks" : sumWeeks,
		"description" : description,
		"image":image
	}
	if(!courseCode||!name||!courseHour||!credit||!sumWeeks||!description||!image) {
		alert("请完善课程信息！");
	} else {
		$.post(CONTEXT_PATH + "course/ajaxCreate", {
			data : JSON.stringify(json)
		}, function(data) {
			if (data.status == "success") {
				deleteBefore(keyList);
				window.parent.location.href = CONTEXT_PATH + "course/list";
			} else {
				alert(data.message);
			}
		});
	}
}
	

</script>
</html>